<?php

include_once 'db.php';

class Parser{
	private $_content;
	private $_db;
	private $_clientid;

	function __construct($file){
		if(file_exists($file)){
			$fp = fopen($file, "r");
			$this->_content = fread($fp, filesize($file));
			fclose($fp);
		}
		else {
			$this->_content = $file;
		}

		$this->_db = new dbConnect();
	}

	public function analysis(){
		if(empty($this->_content)) {
			echo 'WARN: no content to analysis';
			return;
		}

		$result = preg_match_all('/<log.*?<\/log>/s', $this->_content, $matches);
		if(0 == $result) {
			echo 'WARN: can not match log node';
			return;
		}

		foreach($matches[0] as $each){
			if(strpos($each, "<window") > 0 ){
				$this->parseRuntime($each);
			}
			else {
				$this->parseFixed($each);
			}
		}
	}

	function parseRuntime($xml){
		$dom = new DOMDocument();
		$dom->loadXML($xml);

		$root = $dom->documentElement;

		$this->_clientid = $root->getAttribute('clientid');
		$buildtime = $root->getAttribute('buildtime');
		$appversion = $root->getAttribute('appversion');
		$this->writeLogEntry($buildtime, $appversion);

		$this->writeWindows($root);
		$this->writeUiElements($root);
		$profile = $this->writeProfile($root);
		$this->writeInstall($profile);
		$this->writeStability($profile);
	}

	function writeLogEntry($buildtime, $appversion){
		date_default_timezone_set('UTC');
		$now = date("Y-m-d H:i:s");
		
		$sql = "insert into logentry (clientid, buildtime, appversion, time) values ('$this->_clientid', '$buildtime', '$appversion', '$now')";
		$this->_db->execSql($sql);
	}

	function writeWindows($root){
		$windows = $root->getElementsByTagName('window');

		foreach($windows as $each){
			$action = $each->getAttribute('action');
			$windowid = $each->getAttribute('windowid');
			$parent = $each->getAttribute('parent');
			$session = $each->getAttribute('session');
			$time = $each->getAttribute('time');

			$sql = "insert into window (action, windowid, parent, session, time, clientid) values ('$action', '$windowid', '$parent', '$session', '$time', '$this->_clientid')";
			$this->_db->execSql($sql);
		}
	}

	function writeUiElements($root){
		$uis = $root->getElementsByTagName('uielement');

		foreach($uis as $each){
			$action = $each->getAttribute('action');
			$value = $each->getAttribute('value');
			$window = $each->getAttribute('window');
			$session = $each->getAttribute('session');
			$time = $each->getAttribute('time');

			$sql = "insert into uielement (action, value, windowid, session, time, clientid) values ('$action', '$value', '$window', '$session', '$time', '$this->_clientid')";
			$this->_db->execSql($sql);
		}
	}

	function parseFixed($xml) {
		$dom = new DOMDocument();
		$dom->loadXML($xml);

		$root = $dom->documentElement;

		$this->_clientid = $root->getAttribute('clientid');
		$buildtime = $root->getAttribute('buildtime');
		$appversion = $root->getAttribute('appversion');
		$this->writeLogEntry($buildtime, $appversion);
			
		$profile = $this->writeProfile($root);
		$this->writeInstall($profile);
		$this->writeStability($profile);

		$this->writePlugins($profile);
		$this->writeCPU($profile);
		$this->writeMemory($profile);
		$this->writeOS($profile);
		$this->writeGPU($profile);
		$this->writeDisplay($profile);
		$this->writeBookmarks($profile);
		$this->writeKeywords($profile);
	}

	function writeProfile($root){
		$profile = $root->getElementsByTagName('profile')->item(0);
		$session = $profile->getAttribute('session');
		$time = $profile->getAttribute('time');
			
		$sql = "insert into profile (session, time, clientid) values ('$session', '$time', '$this->_clientid')";
		$this->_db->execSql($sql);
		 
		return $profile;
	}

	function writeInstall($profile){
		$install = $profile->getElementsByTagName('install')->item(0);
		$date = $install->getAttribute('installdate');
		$buildID= $install->getAttribute('buildid');
			
		//should be buildid here
			
		$sql = "insert into install (installdate, buildversion, clientid) values ('$date', '$buildID', '$this->_clientid')";
		$this->_db->execSql($sql);
	}

	function writePlugins($profile){
		$root = $profile->getElementsByTagName('plugins')->item(0);
		$plugins = $root->getElementsByTagName('plugin');
			
		foreach($plugins as $item){
			$name = $item->getAttribute('name');
			$fileName = $item->getAttribute('filename');
			$version = $item->getAttribute('version');

			$sql = "insert into plugin (name, filename, version, clientid) values ('$name', '$fileName', '$version', '$this->_clientid')";
			$this->_db->execSql($sql);
		}
	}

	function writeStability($profile){
		$stability = $profile->getElementsByTagName('stability')->item(0);
			
		$launchcount = $stability->getAttribute('launchcount');
		$crashcount = $stability->getAttribute('crahcount');
		$pageloadcount = $stability->getAttribute('pageloadcount');
		$uptimesec = $stability->getAttribute('uptimesec');
		$incompleteshutdowncount = $stability->getAttribute('$incompleteshutdowncount');
		$breakpadregistrationok = $stability->getAttribute('breakpadregistrationok');
		$breakpadregistrationfail = $stability->getAttribute('breakpadregistrationfail');
		$debuggerpresent = $stability->getAttribute('debuggerpresent');
		$debuggernotpresent = $stability->getAttribute('debuggernotpresent');
			
		$sql = "insert into stability (launchcount, crashcount, uptimesec, pageloadcount, incompleteshutdowncount, breakpagregistrationcount, breakpagregistrationfail, debuggerpreset, debuggernotpreset, clientid)
	 		values ('$launchcount', '$crashcount', '$uptimesec', '$pageloadcount', '$incompleteshutdowncount', '$breakpadregistrationok', '$breakpadregistrationfail', '$debuggerpresent', '$debuggernotpresent', '$this->_clientid')";
		$this->_db->execSql($sql);
	}

	function writeCPU($profile){
		$cpu = $profile->getElementsByTagName('cpu')->item(0);
		$arch = $cpu->getAttribute('arch');
			
		$sql = "insert into cpu (arch, clientid) values ('$arch', '$this->_clientid')";
		$this->_db->execSql($sql);
	}

	function writeMemory($profile){
		$memory = $profile->getElementsByTagName('memory')->item(0);
		$mb = $memory->getAttribute('mb');
		$dllbase = $memory->getAttribute('dllbase');
			
		$sql = "insert into memory (capcity, dllbase, clientid) values ('$mb', '$dllbase', '$this->_clientid')";
		$this->_db->execSql($sql);
	}

	function writeOS($profile){
		$os = $profile->getElementsByTagName('os')->item(0);
		$name = $os->getAttribute('name');
		$version = $os->getAttribute('version');
			
		$sql = "insert into os (name, version, clientid) values ('$name', '$version', '$this->_clientid')";
		$this->_db->execSql($sql);
	}

	function writeGPU($profile) {
		$gpu = $profile->getElementsByTagName('gpu')->item(0);
		$vendorid = $gpu->getAttribute('vendorid');
		$deviceid = $gpu->getAttribute('deviceid');
			
		$sql = "insert into gpu (vendorid, deviceid, clientid) values ('$vendorid', '$deviceid', '$this->_clientid')";
		$this->_db->execSql($sql);
	}

	function writeDisplay($profile){
		$display = $profile->getElementsByTagName('display')->item(0);
		$xsize = $display->getAttribute('xsize');
		$ysize = $display->getAttribute('ysize');
		$screens = $display->getAttribute('screens');
			
		$sql = "insert into display (width, height, screencount, clientid) values ('$xsize', '$ysize', '$screens', '$this->_clientid')";
		$this->_db->execSql($sql);
	}

	function writeBookmarks($profile){
		$bms = $profile->getElementsByTagName('bookmarks')->item(0);
		$locs = $bms->getElementsByTagName('bookmarklocation');

		foreach($locs as $item){
			$name = $item->getAttribute('name');
			$folderCount = $item->getAttribute('foldercount');
			$itemCount = $item->getAttribute('itemcount');

			$sql = "insert into bookmark (location, foldercount, itemcount, clientid) values ('$name', '$folderCount', '$itemCount', '$this->_clientid')";
			$this->_db->execSql($sql);
		}
	}

	function writeKeywords($profile){
		$key = $profile->getElementsByTagName('keywords')->item(0);
		$count = $key->getAttribute('count');

		$sql = "insert into keyword (count, clientid) values ('$count', '$this->_clientid')";
		$this->_db->execSql($sql);
	}

	function __destruct(){
		$this->_content = NULL;
		$this->_db->__destruct();
	}
}

?>